package com.baizhi.springmvc.util;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class EmailUtil {
    // 1.设置发送邮件的一些参数
    private static Properties pro = new Properties();

    static {
        pro.setProperty("mail.transport.protocol", "smtp");
        pro.setProperty("mail.smtp.host", "smtp.qq.com");
        pro.setProperty("mail.smtp.auth", "true");
        pro.setProperty("mail.smtp.port", "587");
    }

    public static int sendSimpleMail(String recipients,Integer id) {
        int res = 0;
        try {
            // 2.创建一个会话对象和邮件服务器交互
            Session session = Session.getDefaultInstance(pro);
            // 3.创建一个邮件对象
            MimeMessage message = new MimeMessage(session);

            message.setFrom(new InternetAddress("310681323@qq.com")); // 发送人

            message.setRecipient(MimeMessage.RecipientType.TO,
                    new InternetAddress(recipients)); // 收件人

            message.setSubject("这是标题内容111");// 设置邮件的标题

            message.setContent("<a href='http://localhost:8081/updateUserStatus.do?id="+id+"'>点击激活</a>", "text/html;charset=UTF-8");// 设置邮件的正文

            message.setSentDate(new Date());// 发送时间

            message.saveChanges(); // 保存设置

            Transport transport = session.getTransport();// 获取一个传输对象

            transport.connect("310681323@qq.com", "wjxjttbflefwbhhh");//我的wjxjttbflefwbhhh 设置发送邮件用户名和密码

            transport.sendMessage(message, message.getAllRecipients());// 发送邮件

            transport.close();
            res = 123456;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}
